package org.big.kafkademo;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Service;

@Service
public class KafkaConsumerService {

    // 监听名为 "demo-topic" 的 Topic，并指定消费者组
    @KafkaListener(topics = "demo-topic", groupId = "my-demo-group")
    public void receiveMessage(String message, Acknowledgment ack) {
        try {
            // 在此处处理您的业务逻辑
            System.out.println("接收到消息: " + message);
            // ... 业务处理代码 ...

            // 业务处理成功后，手动提交偏移量 (ack)
            ack.acknowledge();
            System.out.println("消息已确认（ACK）");
        } catch (Exception e) {
            // 处理异常，可根据需要决定是否重新入队
            System.err.println("处理消息时发生错误: " + e.getMessage());
        }
    }
}